using ED.Common;
using ED.Common.BaseModel;
using ED.Common.Extensions;
using ED.Common.Utils;
using ED.Entity;
using ED.IRepository;
using ED.IService;
using ED.Model.Request;
using Serilog;

/// <summary>
/// Employee服务
/// </summary>
namespace ED.Service
{
    public class EmployeeService : IEmployeeService
    {
        private readonly IEmployeeRepository employeeRepository;
        public EmployeeService(IEmployeeRepository employeeRepository)
        {
            this.employeeRepository = employeeRepository;
        }

        /// <summary>
        /// Employee查询
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public PageOut<EmployeeQueryRes> Page(EmployeeQueryReq req)
        {
            Log.Error("我是测试数据，进入ES");
          return  employeeRepository.Page(req);
        }

        /// <summary>
        /// Employee新增
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public ResponseOutput Add(EmployeeOperateReq req)
        {
            // 校验
            var empEntity = employeeRepository.Get(t => t.Name == req.Name || t.Phone == req.Phone || t.Mobile == req.Mobile);
            if (!empEntity.IsDefault())
            {
                ThrowHelp.Throw("员工信息重复");
            }

            var entity = req.Mapper<Base_EmployeeEntity>();
            employeeRepository.Insert(entity);
            return ResponseOutput.Ok();
        }

        /// <summary>
        /// Employee更新
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public ResponseOutput Update(EmployeeOperateReq req)
        {
            // 校验
            var empEntity = employeeRepository.Get(t => (t.Name == req.Name || t.Phone == req.Phone || t.Mobile == req.Mobile) && t.Id != req.Id);
            if (!empEntity.IsDefault())
            {
                ThrowHelp.Throw("员工信息重复");
            }

            var entity = employeeRepository.Get(t => t.Id == req.Id);
            // 这一步需要验证
            entity = CommonUtils.CopyProperties(req, entity);
            employeeRepository.Update(entity);
            return ResponseOutput.Ok();
        }

        /// <summary>
        /// Employee删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public ResponseOutput Delete(int Id)
        {
            employeeRepository.Delete(Id);
            return ResponseOutput.Ok();

        }
    }
}
